//
//  FavouriteViewController.m
//  DuBai
//
//  Created by darktm on 14-2-23.
//  Copyright (c) 2014年 darktm. All rights reserved.
//

#import "FavouriteViewController.h"
#import "MyFavouriteViewCell.h"
#import "favourite.h"

#import "TaskWithGetAttentions.h"
#import "TaskWithCancelFavourite.h"

#import "UIImageView+WebCache.h"

@interface FavouriteViewController ()<WYHttpTaskDelegate>

@property(nonatomic,strong)NSMutableArray* favouriteArray;

@end

@implementation FavouriteViewController


#define TAG_CELL 10000

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        self.title = @"我的关注";
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the views
    
//    [self.cv registerClass:[BestCollectionCell class] forCellWithReuseIdentifier:@"my_cell"];
    UINib *nib = [UINib nibWithNibName:@"MyFavouriteViewCell" bundle:nil];
    [self.cv registerNib:nib forCellWithReuseIdentifier:@"MyFavouriteViewCell"];
    
    [self loadFavourites];
}


#pragma mark - UICollectionView delegate -

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    int count = ceilf(self.favouriteArray.count / 2.0f);
    if(section == count - 1)
        return self.favouriteArray.count - 2 * section;
    else
        return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MyFavouriteViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyFavouriteViewCell" forIndexPath:indexPath];
    cell.layer.borderWidth = 1;
    cell.layer.borderColor = [[[UIColor brownColor] colorWithAlphaComponent:.2] CGColor];
    
    int index = indexPath.section * 2 + indexPath.item;
    
    favourite *b = nil;
    

    b = self.favouriteArray[index];
    if(b.icon)
        [cell.image setImageWithURL:[NSURL URLWithString:[IMAGE_BASE_URL stringByAppendingString:b.icon]]];
    cell.desc.text = b.desc;
    cell.hots.text = b.hots;
    cell.price.text = b.price;

    [cell.btn addTarget:self action:@selector(on_btn_unfavourite:) forControlEvents:UIControlEventTouchUpInside];
    cell.btn.tag = TAG_CELL + index;
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    int count = ceilf(self.favouriteArray.count / 2.0f);
    return count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    int index = indexPath.section * 2 + indexPath.item;
    
    DLog(@"%@,",indexPath);
}


#pragma mark - http delegate -

-(void)requestCompleteWithObj:(WYHttpTask *)obj{
    
    
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    
    id rsult = [obj parese];
    
    if([obj isKindOfClass:[TaskWithGetAttentions class]]){
        
        if(self.favouriteArray){
            [self.favouriteArray addObjectsFromArray:rsult];
        }else{
            self.favouriteArray = rsult;
        }
    }else if([obj isKindOfClass:[TaskWithCancelFavourite class]]){
        
        TaskWithCancelFavourite *f = (TaskWithCancelFavourite *)obj;
        NSString *aid = f.aid;
        
        for (favourite *fa in self.favouriteArray) {
            
            if([aid isEqualToString:fa.aid]){
                [self.favouriteArray removeObject:fa];
                break;
            }
        }
    }
    
    [self.cv reloadData];
}
-(void)requestFailWithMsg:(WYHttpTask *)obj{
    DLog(@"%@",obj.networkOp.url);
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - private method -

-(void)loadFavourites{
    
    TaskWithGetAttentions *attention = [[TaskWithGetAttentions alloc] init];
    attention.delegate = self;
    [attention start];
}
-(void)cancelFavouriteWithId:(NSString *)aid{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    TaskWithCancelFavourite *c = [[TaskWithCancelFavourite alloc] init];
    c.aid = aid;
    c.delegate = self;
    [c start];
}

-(void)on_btn_unfavourite:(UIButton *)btn{
    
    favourite *f = self.favouriteArray[btn.tag - TAG_CELL];
    [self cancelFavouriteWithId:f.aid];
}

@end
